package com.happy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.dao.SubjectMapper;
import com.happy.entity.SubjectEntity;
import com.happy.service.ISubjectService;
import com.happy.util.StateEnum;
import com.happy.util.StateException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 17:25 2021/1/31
 * @return
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, SubjectEntity> implements ISubjectService {

    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public List<SubjectEntity> queryList ( SubjectEntity subjectEntity ) {
        if ( subjectEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        List<SubjectEntity> subjectEntities = subjectMapper.queryList ( subjectEntity, ( subjectEntity.getPageNum () - 1 ) * subjectEntity.getPageSize (), subjectEntity.getPageSize () );
        return subjectEntities;
    }

    @Override
    public int totalCount ( SubjectEntity subjectEntity ) {
        int i = subjectMapper.totalCount ( subjectEntity );
        return i;
    }

    @Override
    public SubjectEntity querySingle ( SubjectEntity subjectEntity ) {
        QueryWrapper<SubjectEntity> queryWrapper = new QueryWrapper<> ();
        if ( subjectEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( subjectEntity.getSubjectId () != null ) {
            queryWrapper.eq ( "subjectId", subjectEntity.getSubjectId () );
        }
        if ( subjectEntity.getOptionId () != null ) {
            queryWrapper.eq ( "optionId", subjectEntity.getOptionId () );
        }
        if ( subjectEntity.getPaperId () != null ) {
            queryWrapper.eq ( "paperId", subjectEntity.getPaperId () );
        }
        subjectEntity = baseMapper.selectOne ( queryWrapper );
        return subjectEntity;
    }

    @Override
    public SubjectEntity addSingle ( SubjectEntity subjectEntity ) {
        if ( subjectEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        subjectEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( subjectEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return subjectEntity;
    }

    /**
     * 修改题目信息
     *
     * @param subjectEntity
     * @return
     */
    @Override
    public SubjectEntity updateSingle ( SubjectEntity subjectEntity ) {
        if ( subjectEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( subjectEntity.getSubjectId () == null ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        subjectEntity.setChangeTime ( new Date () );
        int i = baseMapper.updateById ( subjectEntity );
        if ( i == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return subjectEntity;
    }
}
